package com.qijian.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @author Alex
 * 发送短信工具
 */
public class SendSmsUtil {
    // 区域
    private final static String AREA = "cn-hangzhou";
    // ID
    private final static String ACCESSKEY_ID = "LTAI4GKmpm85KrwJZ2sAmx1k";
    // key
    private final static String ACCESSKEY_SECRET = "GJw4TL1YpgBu7EKz2aNOtRahkkoCZ4";
    // 短信签名
    private final static String SIGN_NAME = "振江新能源";
    // 发送验证码模版
    private final static String TEMPLATE_CODE = "SMS_205891456";

    // 定时任务异常
    public final static String QUARTZ_ERROR = "SMS_463206229";

    // redis缓存key
    public final static String SMS_PREFIX = "user::sms:";

    public static boolean sendSms(String phone, String code, String outId) {
        DefaultProfile profile = DefaultProfile.getProfile(AREA, ACCESSKEY_ID, ACCESSKEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        JSONObject json = new JSONObject();
        json.put("code", code);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", AREA);
        //手机
        request.putQueryParameter("PhoneNumbers", phone);
        //签名
        request.putQueryParameter("SignName", SIGN_NAME);
        //模版
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
        //参数(json)
        request.putQueryParameter("TemplateParam", json.toString());
        //上传流水ID
        if (outId != null) {
            request.putQueryParameter("OutId", outId);
        }
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String data = response.getData();
            return StringUtils.isNotBlank(data) && ("OK".equals(JSON.parseObject(data).getString("Code")));
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static boolean sendSms(String phone, String templateCode, String outId, JSONObject param) {

        if(StringUtils.isEmpty(phone)) {
            return false;
        }
        DefaultProfile profile = DefaultProfile.getProfile(AREA, ACCESSKEY_ID, ACCESSKEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", AREA);
        //手机
        request.putQueryParameter("PhoneNumbers", phone);
        //签名
        request.putQueryParameter("SignName", SIGN_NAME);
        //模版
        request.putQueryParameter("TemplateCode", templateCode);
        //参数(json)
        request.putQueryParameter("TemplateParam", param.toJSONString());
        //上传流水ID
        if (outId != null) {
            request.putQueryParameter("OutId", outId);
        }
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            String data = response.getData();
            return StringUtils.isNotBlank(data) && ("OK".equals(JSON.parseObject(data).getString("Code")));
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
